package com.qianfeng.camerademo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnItemClick;
import com.qianfeng.camerademo.adapter.ShowImageAdapter;
import com.qianfeng.camerademo.adapter.ShowImageAdapter.ViewHolder;
import com.qianfeng.camerademo.bean.UploadImageBean;
import com.qianfeng.camerademo.utils.FileUtils;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.LayoutParams;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

/**
 * @author: 张唯
 * @类 说 明:
 * @version 1.0
 * @创建时间：2014-10-28 下午5:24:36
 * 
 */
@ContentView(R.layout.activity_show)
public class ShowImageActivity extends ActionBarActivity {
	private static final String url = "http://10.0.163.145:8080/app/uplaodFile?method=upload";
	@ViewInject(R.id.show_gridView)
	private GridView gridView;

	private ArrayList<UploadImageBean> bitmaps = new ArrayList<UploadImageBean>();

	private ShowImageAdapter adapter;
	// 图片处理操作类
	private BitmapFactory.Options options = new BitmapFactory.Options();
	private HttpUtils httpUtils;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		initActionBar();
		httpUtils = new HttpUtils();
		getImageFile();
	}

	/**
	 * 初始化acitionbar相关设置
	 * 
	 * @Title: initActionBar
	 * @说 明:
	 * @参 数:
	 * @return void 返回类型
	 * @throws
	 */
	private void initActionBar() {
		ActionBar actionBar = getSupportActionBar();
		// logo右边的返回箭头
		actionBar.setDisplayHomeAsUpEnabled(false);
		// 设置是否显示logo
		actionBar.setDisplayShowHomeEnabled(false);
		// 设置是否显示logo右边的标题
		actionBar.setDisplayShowTitleEnabled(false);

		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		// 可以设置自定义的Actionbar布局
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setCustomView(createActionBarView(), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	}

	private View createActionBarView() {
		View view = getLayoutInflater().inflate(R.layout.top_layout, null);
		ActionBarHolder holder = new ActionBarHolder();
		ViewUtils.inject(holder, view);
		holder.uploadBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				uploadImages();
			}
		});
		return view;
	}

	private class ActionBarHolder {
		@ViewInject(R.id.return_btn)
		Button retrunBtn;
		@ViewInject(R.id.upload_btn)
		Button uploadBtn;
		@ViewInject(R.id.title)
		TextView title;

		@OnClick({ R.id.return_btn, R.id.upload_btn })
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.return_btn:
				break;
			case R.id.upload_btn:
				uploadImages();
				break;
			}
		}

	}

	/**
	 * 第一步 获得保存图片文件夹 第二步 遍历这个文件获取里面所有文件 第三步 通过 BitmapFactory将文件转化Bitmap;
	 * 
	 * @Title: getImageFile
	 * @说 明:
	 * @参 数:
	 * @return void 返回类型
	 * @throws
	 * 
	 */
	@SuppressLint("NewApi")
	public void getImageFile() {
		File imageCache = FileUtils.getImageCache();
		options.inSampleSize = 10;
		if (imageCache != null) {
			File[] imageFiles = imageCache.listFiles();
			if (imageFiles != null && imageFiles.length > 0) {
				for (File file : imageFiles) {
					
					Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
					if (bitmap != null && bitmap.getByteCount() > 0) {
						bitmaps.add(new UploadImageBean(bitmap, false, file));
					}
				}
			}
		}
		adapter = new ShowImageAdapter(bitmaps, this);
		gridView.setAdapter(adapter);
	}

	/**
	 * 如果你的item里有button checkbox 这些控件优先获取焦点
	 * 
	 * @Title: onItemClick
	 * @说 明:
	 * @参 数: @param parent
	 * @参 数: @param view
	 * @参 数: @param position
	 * @参 数: @param id
	 * @return void 返回类型
	 * @throws
	 * 
	 */
	@OnItemClick(R.id.show_gridView)
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		ViewHolder vh = (ViewHolder) view.getTag();
		UploadImageBean bean = bitmaps.get(position);
		if (!bean.isShow()) {
			vh.cb.setChecked(true);
			bean.setShow(true);
		} else {
			vh.cb.setChecked(false);
			bean.setShow(false);
		}
	}

	/**
	 * 文件上传
	 * 
	 * @Title: uploadImages
	 * @说 明:
	 * @参 数:
	 * @return void 返回类型
	 * @throws post
	 *             get 区别 上传大文件 文档
	 */
	public void uploadImages() {
		RequestParams params = new RequestParams();
		if (bitmaps != null && !bitmaps.isEmpty()) {
			for (int i = 0; i < bitmaps.size(); i++) {
				if (bitmaps.get(i).isShow()) {
					params.addBodyParameter(i + "", bitmaps.get(i).getImageFile());
				}
			}
		}
		
		httpUtils.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				System.out.println(responseInfo.result);
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				System.out.println("上传失败");
			}
		});
	}

}
